//
//  QPRRefreshDataCoverView.m
//  qpyd
//
//  Created by 掌游网络 on 2017/9/7.
//  Copyright © 2017年 掌游网络. All rights reserved.
//

#import "QPRRefreshDataCoverView.h"

@implementation QPRRefreshDataCoverView {
    
    UILabel *_contentLabel;
    UIButton *_refreshBtn;
    UIActivityIndicatorView *_indicatorView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.oy_height / 2 - OYAppNavBarH, 240, 20)];
    _contentLabel.oy_centerX = self.oy_width / 2;
    _contentLabel.textColor = OYRGBFrom16(0x999999);
    _contentLabel.font = [UIFont systemFontOfSize:13];
    _contentLabel.textAlignment = NSTextAlignmentCenter;
    _contentLabel.text = @"加载失败，请检查网络";
    [self addSubview:_contentLabel];
    _refreshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _refreshBtn.frame = CGRectMake(0, _contentLabel.oy_bottom - 5, 60, 40);
    _refreshBtn.oy_centerX = _contentLabel.oy_centerX;
    [_refreshBtn setTitle:@"重试" forState:UIControlStateNormal];
    [_refreshBtn setTitleColor:OYRGBFrom16(0x3695D9) forState:UIControlStateNormal];
    _refreshBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:_refreshBtn];
    [_refreshBtn addTarget:self action:@selector(refreshClick) forControlEvents:UIControlEventTouchUpInside];
    
    _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self addSubview:_indicatorView];
    _indicatorView.center = CGPointMake(self.oy_width / 2, self.oy_height / 2 - OYAppNavBarH);
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    
    _indicatorView.center = CGPointMake(self.oy_width / 2, self.oy_height / 2 - OYAppNavBarH);
}

- (void)setLabelColor:(UIColor *)labelColor {
    _labelColor = labelColor;
    
    _contentLabel.textColor = labelColor;
}

- (void)setButtonColor:(UIColor *)buttonColor {
    _buttonColor = buttonColor;
    
    [_refreshBtn setTitleColor:buttonColor forState:UIControlStateNormal];
}

- (void)refreshClick {
    
    _contentLabel.hidden = YES;
    _refreshBtn.hidden = YES;
    
    [_indicatorView startAnimating];
    
    !self.refreshBtnClickBlock ?: self.refreshBtnClickBlock();
}

#pragma mark - public method
/**
 开始刷新
 */
- (void)beginRefresh {
    self.hidden = NO;
    _contentLabel.hidden = YES;
    _refreshBtn.hidden = YES;
    
    [_indicatorView startAnimating];
}
/**
 刷新成功
 */
- (void)refreshSuccess {
    
    [UIView animateWithDuration:0.15 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        self.hidden = YES;
        self.alpha = 1;
        _contentLabel.hidden = NO;
        _refreshBtn.hidden = NO;
        [_indicatorView stopAnimating];
    }];
}
/**
 刷新失败
 */
- (void)refreshFailure {
    self.hidden = NO;
    _contentLabel.hidden = NO;
    _refreshBtn.hidden = NO;
    [_indicatorView stopAnimating];
}


@end
